<template>
  <div class="content">
    <el-card class="box-card">
      <template #header>
        计划单详情
      </template>
      <el-form :model="info" class="grids columnsLine4 formLine">
        <el-form-item label="盘点单号">
          {{info.inventoryNo}}
        </el-form-item>
        <el-form-item label="供应商">
          <selectSupplier v-model="info.supplierSn" val="supplierSn" readonly="1"></selectSupplier>
        </el-form-item>
        <el-form-item label="盘点工厂">
          <selectFactory v-model="info.factoryId" readonly="1"></selectFactory>
        </el-form-item>
        <el-form-item label="盘点仓库">
          <selectStores v-model="info.storeId" readonly="1"></selectStores>
        </el-form-item>
        <el-form-item label="状态">
          {{counter.idString_format("inventoryStatus",info.status)}}
        </el-form-item>
        <el-form-item label="创建人">
          {{info.createBy}}
        </el-form-item>
        <el-form-item label="创建时间">
          {{info.createTime}}
        </el-form-item>
        <el-form-item label="备注">
          {{info.remark}}
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card">
      <template #header>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="货物名称">
            <el-input v-model="formInline.cargoName" placeholder="请输入货物名称" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">查找</el-button>
            <el-button type="primary" plain @click="dialogVisible=true">新增</el-button>
            <el-button @click="init">重置</el-button>
          </el-form-item>
        </el-form>
      </template>

      <el-table :data="tableParams.rows">
        <el-table-column label="货物编码" align="center" width="200" prop="cargoSn" />
        <el-table-column prop="cargoName" label="货物名称" />
        <el-table-column label="库区">
          <template #default="scope">
            <selectStoreRegion v-model="scope.row.storeRegionId" readonly="1"></selectStoreRegion>
          </template>
        </el-table-column>
        <el-table-column label="库位">
          <template #default="scope">
            <selectStoreLocation v-model="scope.row.storeRegionLocationId" readonly="1"></selectStoreLocation>
          </template>
        </el-table-column>
        <el-table-column prop="plannedNum" label="账面数量" />
        <el-table-column prop="num" label="实盘数量" />
        <el-table-column prop="createTime" label="盈亏数量">
          <template #default="scope">
            {{Math.abs(scope.row.plannedNum - scope.row.num)||0}}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center" fixed="right" width="160">
          <template #default="scope">
            <el-button type="primary" text @click="handClick(scope.row,1)">编辑</el-button>
            <el-button type="primary" text @click="handClick(scope.row,2)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination v-if="tableParams['total']" :total="tableParams['total']" v-model:page="formInline.pageNum" v-model:limit="formInline.pageSize" @pagination="getInfo" />

      <el-dialog v-model="dialogVisible" title="新增/编辑" width="500" :before-close="handleClose">
        <span>This is a message</span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">Cancel</el-button>
            <el-button type="primary" @click="dialogVisible = false">
              Confirm
            </el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();
import { useRoute } from "vue-router";
const route = useRoute();

let info = ref({});
let tableParams = ref({
  pageNum: 1,
  pageSize: 10,
  cargoName: "",
});
let formInline = ref({
  pageNum: 1,
  pageSize: 10,
  inventoryNo: "",
  cargoName: "",
});
let dialogVisible = ref(false);

onMounted(() => {
  getDetails(route.query.id);
  search();
});

let getDetails = async (id) => {
  let { data } = await proxy.$apis.inventoryRecordDetails(id);
  info.value = data;
};
let getInfo = async () => {
  formInline.value.inventoryNo = info.value.inventoryNo;
  tableParams.value = await proxy.$apis.inventoryRecordDetailList(
    formInline.value
  );
  console.log("货物", tableParams.value.rows);
};
let init = () => {
  formInline.value = {
    pageNum: 1,
    pageSize: 10,
    cargoName: "",
  };
  getInfo();
};
let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let handleClose = () => {
  dialogVisible.value = false;
};
</script>

<style lang="scss" scoped>
.content {
  margin: 20px;
  .box-card {
    margin-bottom: 15px;
  }
  .formLine {
    gap: 0px 12px;
    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
